// 页面基类
module.exports = Behavior({
  properties: {
  },
  data: {
    options:{},//页面 onLoad 时的参数
    host: 'https://flower.kmmyxb.cn',
   
    wx_login: false, //是否显示微信登陆框
    userInfo:{//用户数据
      apply_status:'',
      avatarUrl:'',
      balance:0,
      city:'',
      name:'',
      nickname:'',
      sum:'',
      tel:'',
      user_id:0
    },
    contact:{
      tel:'400-'
    },//联系客服电话，微信，坐标 
  },
  created: function () {
  },
  attached: function () {
  },
  ready: function () {
    this.login_reload()
  },

  methods: {
  /**登陆状态更新之后重载的方法 */
  login_reload() {
      console.log("执行基类重载方法")
      this.data.userInfo=getApp().globalData.userInfo;
    },
    /**拨打客服电话 */
    call_phone({currentTarget}){
      wx.makePhoneCall({
        phoneNumber: currentTarget.dataset.tel||this.data.contact.tel //仅为示例，并非真实的电话号码
      })
    },
    /**隐藏弹窗通用方法 */
    hide_popup({currentTarget}){
      let obj={};
      obj[currentTarget.dataset.field]=false;
      this.setData(obj)
    },
    // 微信登陆
    getUserInfo(event) {
      let that = this;
      wx.login({
        withCredentials:true,
        timeout: 10000,
        success(res) {
          if (res.code) {
            let code=res.code
            wx.getUserInfo({
              withCredentials: true,
              lang: 'zh_CN',
              success: function (res) {
                console.log('获取私密信息', res)
                that.request({
                  method: 'GET',
                  url: that.data.host + '/home/login',
                  data: {
                    code:code,
                    encryptedData: res.encryptedData,
                    iv: res.iv
                  }
                },res=>{
                  wx.showToast({
                    title: '登陆成功',
                  })
                  console.log('后台返回的用户数据', res);
                  wx.setStorage({
                    data: res.data.access_token,
                    key: 'Authorization',
                  })
                  getApp().globalData.userInfo={
                    face:res.data.face,
                    nickname:res.data.nickname,
                    uid:res.data.uid,
                  };
                  that.data.userInfo= {
                    
                  };
                  that.login_reload()
                });
              },
              fail:function (res) {
                 wx.showToast({
                  icon: 'none',
                  title: '获取微信信息失败',
                })
              }
            })
           
          } else {
            wx.showToast({
              icon: 'none',
              title: '登录失败！' + res.errMsg,
            })
          }
        },
        fail(res) {
          wx.showToast({
            icon: 'none',
            title: res.errMsg || '获取微信授权失败',
          })
        }
      })
      
    },

    /**
     * 网络请求封装
     * @param {*} req_data  
     * url 请求地址,
     * loadingTip:加载中提示，如果存在，先显示loading
     * login:true 需要登录状态
     * @param {*} success_func 成功回调方法
     * @param {*} fail_func 失败回调方法
     * @param {*} complete 最终执行的方法
     */
    request: function (req_data, success_func, fail_func = null, complete = null) {
      let that = this;
      let Authorization= wx.getStorageSync('Authorization');
      if(req_data.login){
        if(!Authorization){
          this.setData({wx_login:true});
          return;
        }
      }
      if(req_data.loadingTip){
        wx.showLoading({
          title: req_data.loadingTip,
          mask:true
        })
      }
      req_data.header = {
        'content-type': 'application/json',
        'Accept': 'application/json, text/javascript, */*; q=0.01'
      };
    
      if(Authorization){
        req_data.header.Authorization=Authorization;
      }
      req_data.success = function (res) { //成功处理
        if (res.statusCode == 200) {
          if(res.header.Authorization){
            wx.setStorage({
              data: res.header.Authorization,
              key: 'Authorization',
            })
          }
          let data = res.data;
          if (data.code == 0) {
            if(data.result){
              success_func(data.result);
            }else{
              success_func(data);
            }
          } else {
            wx.showToast({
              title:data.result|| data.msg + "",
              icon: 'none'
            })
          }
        } else if (res.statusCode == 401) {
          that.setData({
            wx_login: true
          })
        } else {
          wx.showToast({
            title: res.data|| "网络异常" ,
            icon: 'none'
          })
          // wx.showToast({
          //   title: "网络异常：" + (res.data.message|res.data),
          //   icon: 'none'
          // })
        }
      };
      req_data.fail = function (e) { //失败处理
        if (fail_func) {
          fail_func(e);
        } else {
      
          wx.showToast({
            title: '网络异常：' + e.errMsg,
            icon: 'none'
          })
        }
      }
      req_data.complete = function (e) { //失败处理
        if(req_data.loadingTip){
          wx.hideLoading({
            success: (res) => {},
          })
        }
        if (complete) {
          complete(e);
        }
      }
      wx.request(req_data);
    },

  }
})